<template>
  <div class="order-detail">
    <h2>订单详情</h2>
    
    <el-card class="order-info">
      <template #header>
        <div class="card-header">
          <span>订单信息</span>
          <el-tag :type="orderStatusType">{{ orderStatusText[order.status] }}</el-tag>
        </div>
      </template>
      
      <div class="info-item">
        <span class="label">订单编号：</span>
        <span>{{ order.id }}</span>
      </div>
      <div class="info-item">
        <span class="label">下单时间：</span>
        <span>{{ formatDate(order.createTime) }}</span>
      </div>
      <div class="info-item">
        <span class="label">支付方式：</span>
        <span>{{ paymentMethodText[order.paymentMethod] }}</span>
      </div>
    </el-card>
    
    <el-card class="shipping-info">
      <template #header>
        <div class="card-header">
          <span>收货信息</span>
        </div>
      </template>
      
      <div class="info-item">
        <span class="label">收货人：</span>
        <span>{{ order.shipping?.receiver }}</span>
      </div>
      <div class="info-item">
        <span class="label">联系电话：</span>
        <span>{{ order.shipping?.phone }}</span>
      </div>
      <div class="info-item">
        <span class="label">收货地址：</span>
        <span>{{ order.shipping?.address }}</span>
      </div>
    </el-card>
    
    <el-card class="logistics-info" v-if="order.status !== 'PENDING'">
      <template #header>
        <div class="card-header">
          <span>物流信息</span>
        </div>
      </template>
      
      <template #default>
        <shipping-tracking :order-id="order.id" />
      </template>
    </el-card>
    
    <el-card class="order-items">
      <template #header>
        <div class="card-header">
          <span>商品信息</span>
        </div>
      </template>
      
      <el-table :data="order.items" style="width: 100%">
        <el-table-column label="商品" min-width="400">
          <template #default="scope">
            <div class="product-info">
              <el-image :src="scope.row.product.imageUrl" :alt="scope.row.product.name" />
              <span>{{ scope.row.product.name }}</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="price" label="单价" width="120">
          <template #default="scope">
            ¥{{ scope.row.price }}
          </template>
        </el-table-column>
        <el-table-column prop="quantity" label="数量" width="120" />
        <el-table-column label="小计" width="120">
          <template #default="scope">
            ¥{{ scope.row.price * scope.row.quantity }}
          </template>
        </el-table-column>
      </el-table>
      
      <div class="order-total">
        总计：<span class="total-price">¥{{ order.totalAmount }}</span>
      </div>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, computed, onMounted } from 'vue'
import { useRoute } from 'vue-router'
import { useStore } from 'vuex'
import { ElMessage } from 'element-plus'
import { formatDate } from '@/utils/date'
import ShippingTracking from '@/components/ShippingTracking.vue'

const route = useRoute()
const store = useStore()
const order = ref({})

const orderStatusText = {
  PENDING: '待付款',
  PAID: '待发货',
  SHIPPED: '待收货',
  COMPLETED: '已完成',
  CANCELLED: '已取消'
}

const orderStatusType = computed(() => {
  const statusMap = {
    PENDING: 'warning',
    PAID: 'info',
    SHIPPED: 'primary',
    COMPLETED: 'success',
    CANCELLED: 'danger'
  }
  return statusMap[order.value.status] || 'info'
})

const paymentMethodText = {
  ALIPAY: '支付宝',
  WECHAT: '微信支付'
}

const logisticsCompanyText = {
  SF: '顺丰快递',
  ZTO: '中通快递',
  YTO: '圆通快递'
}

const loadOrder = async () => {
  try {
    const orderId = route.params.orderId
    const response = await store.dispatch('order/getOrderDetail', orderId)
    order.value = response
  } catch (error) {
    ElMessage.error('加载订单详情失败')
  }
}

onMounted(() => {
  loadOrder()
})
</script>

<style scoped>
.order-detail {
  padding: 20px;
  max-width: 1200px;
  margin: 0 auto;
}

.el-card {
  margin-bottom: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.info-item {
  margin: 10px 0;
}

.label {
  color: #666;
  margin-right: 10px;
}

.product-info {
  display: flex;
  align-items: center;
}

.product-info .el-image {
  width: 60px;
  height: 60px;
  margin-right: 10px;
}

.order-total {
  text-align: right;
  margin-top: 20px;
  padding-top: 20px;
  border-top: 1px solid #eee;
}

.total-price {
  color: #f56c6c;
  font-size: 20px;
  font-weight: bold;
}
</style> 